﻿using System;
using KKSG;
using UnityEngine;
using XUtliPoolLib;

namespace XMainClient
{
	// Token: 0x02000FB8 RID: 4024
	internal class XActionReceiver : XSingleton<XActionReceiver>
	{
		// Token: 0x0600D12E RID: 53550 RVA: 0x00305C68 File Offset: 0x00303E68
		public void OnActionReceived(XEntity entity, StepSyncData data)
		{
			XStateDefine xstateDefine = (XStateDefine)(data.Common >> 12 & 15);
			Vector3 pos = new Vector3((float)(data.PosXZ >> 16) / 100f, 0f, (float)(data.PosXZ & 65535) / 100f);
			Vector3 face = XSingleton<XCommon>.singleton.FloatToAngle((float)(data.Common & 4095));
			uint num = (uint)(data.Common >> 16);
			if (xstateDefine != XStateDefine.XState_Skill && entity.Skill != null && entity.Skill.IsCasting())
			{
				entity.Skill.EndSkill(false, true);
			}
			switch (xstateDefine)
			{
			case XStateDefine.XState_Idle:
				entity.Net.OnIdle();
				break;
			case XStateDefine.XState_Move:
			{
				if (entity.Machine.Current != XStateDefine.XState_Move)
				{
					entity.Machine.Stop();
					if (entity.Net.SyncSequence != num)
					{
						XMoveEventArgs @event = XEventPool<XMoveEventArgs>.GetEvent();
						@event.Speed = (float)data.Velocity / 10f;
						@event.Destination = entity.MoveObj.Position;
						@event.Inertia = false;
						@event.Firer = entity;
						XSingleton<XEventMgr>.singleton.FireEvent(@event);
					}
				}
				entity.Net.CorrectMoveSpeed((float)data.Velocity / 10f);
				break;
			}
			case XStateDefine.XState_Freeze:
			{
				if (entity.IsPlayer)
				{
					XSingleton<XActionSender>.singleton.Empty();
				}
				if (entity.Net.SyncSequence != num)
				{
					XEntity entity2 = XSingleton<XEntityMgr>.singleton.GetEntity(data.OpposerID);
					if (entity2 != null)
					{
						if (data.FreezedFromHit)
						{
							XSkillCore skill = entity2.SkillMgr.GetSkill((uint)data.Skillid);
							if (skill != null && skill.Soul.Hit != null && skill.Soul.Hit.Count > data.HitIdx)
							{
								XHitData hitData = skill.Soul.Hit[data.HitIdx];
								XFreezeEventArgs event2 = XEventPool<XFreezeEventArgs>.GetEvent();
								event2.HitData = hitData;
								event2.Dir = Vector3.forward;
								event2.Firer = entity;
								XSingleton<XEventMgr>.singleton.FireEvent(event2);
							}
						}
						else
						{
							XFreezeEventArgs event3 = XEventPool<XFreezeEventArgs>.GetEvent();
							event3.Present = data.PresentInFreezed;
							event3.Dir = Vector3.forward;
							event3.Firer = entity;
							XSingleton<XEventMgr>.singleton.FireEvent(event3);
						}
					}
				}
				break;
			}
			case XStateDefine.XState_BeHit:
			{
				if (entity.IsPlayer)
				{
					XSingleton<XActionSender>.singleton.Empty();
				}
				if (entity.Net.SyncSequence != num)
				{
					XEntity entity3 = XSingleton<XEntityMgr>.singleton.GetEntity(data.OpposerID);
					if (entity3 != null)
					{
						XSkillCore skill2 = entity3.SkillMgr.GetSkill((uint)data.Skillid);
						if (skill2 != null && skill2.Soul.Hit != null && skill2.Soul.Hit.Count > data.HitIdx)
						{
							entity.Machine.Stop();
							XHitData hitData2 = skill2.Soul.Hit[data.HitIdx];
							entity.Machine.Stop();
							XBeHitEventArgs event4 = XEventPool<XBeHitEventArgs>.GetEvent();
							event4.DepracatedPass = true;
							event4.HitDirection = Vector3.forward;
							event4.HitData = hitData2;
							event4.Firer = entity;
							event4.HitFrom = entity3;
							event4.Paralyze = (float)data.HitParalyzeFactor / 100f;
							event4.ForceToFlyHit = data.HitForceToFly;
							XSingleton<XEventMgr>.singleton.FireEvent(event4);
						}
					}
				}
				break;
			}
			case XStateDefine.XState_Death:
			{
				if (entity.IsPlayer)
				{
					XSingleton<XActionSender>.singleton.Empty();
				}
				break;
			}
			case XStateDefine.XState_Skill:
			{
				int num2 = data.SkillCommon & 255;
				//bool flag10 = num2 == 255;
				if (num2 == 255)
				{
					num2 = -1;
				}
				float num3 = (float)(data.SkillCommon >> 8 & 255) / 100f;
				float num4 = (float)(data.SkillCommon >> 16 & 255) / 10f;
				float num5 = (float)(data.SkillCommon >> 24 & 255) / 100f;
				//bool flag11 = entity.Net.SyncSequence != num;
				if (entity.Net.SyncSequence != num)
				{
					XEntity target = data.OpposerIDSpecified ? XSingleton<XEntityMgr>.singleton.GetEntity(data.OpposerID) : null;
					bool flag12 = false;
					XJAComboSkill xjacomboSkill = entity.Skill.CurrentSkill as XJAComboSkill;
					//bool flag13 = xjacomboSkill != null;
					if (xjacomboSkill != null)
					{
						uint num6 = (xjacomboSkill.MainCore.Soul.Ja == null || xjacomboSkill.MainCore.Soul.Ja.Count == 0) ? 0U : XSingleton<XCommon>.singleton.XHash(xjacomboSkill.MainCore.Soul.Ja[0].Name);
						//bool flag14 = num6 == (uint)data.Skillid;
						if (num6 == (uint)data.Skillid)
						{
							xjacomboSkill.ReFire((uint)data.Skillid, target, num2, num4, num);
							flag12 = true;
						}
					}
					//bool flag15 = !flag12;
					if (!flag12)
					{
						entity.Skill.EndSkill(false, true);
						XAttackEventArgs event5 = XEventPool<XAttackEventArgs>.GetEvent();
						event5.Target = target;
						event5.Identify = (uint)data.Skillid;
						event5.Firer = entity;
						event5.Slot = num2;
						event5.TimeScale = num4;
						event5.SyncSequence = num;
						XSingleton<XEventMgr>.singleton.FireEvent(event5);
					}
					//bool flag16 = num3 == 0f;
					if (num3 == 0f)
					{
						//bool flag17 = data.Skillid == (int)entity.SkillMgr.GetBrokenIdentity();
						if (data.Skillid == (int)entity.SkillMgr.GetBrokenIdentity())
						{
							XArmorBrokenArgs event6 = XEventPool<XArmorBrokenArgs>.GetEvent();
							event6.Firer = entity;
							XSingleton<XEventMgr>.singleton.FireEvent(event6);
							XArmorBrokenArgs event7 = XEventPool<XArmorBrokenArgs>.GetEvent();
							event7.Firer = XSingleton<XGame>.singleton.Doc;
							event7.Self = entity;
							XSingleton<XEventMgr>.singleton.FireEvent(event7);
						}
						else
						{
							//bool flag18 = data.Skillid == (int)entity.SkillMgr.GetRecoveryIdentity();
							if (data.Skillid == (int)entity.SkillMgr.GetRecoveryIdentity())
							{
								XArmorRecoverArgs event8 = XEventPool<XArmorRecoverArgs>.GetEvent();
								event8.Firer = entity;
								XSingleton<XEventMgr>.singleton.FireEvent(event8);
								XArmorRecoverArgs event9 = XEventPool<XArmorRecoverArgs>.GetEvent();
								event9.Firer = XSingleton<XGame>.singleton.Doc;
								event9.Self = entity;
								XSingleton<XEventMgr>.singleton.FireEvent(event9);
							}
						}
					}
				}
				//bool flag19 = num5 > 0f && entity.Skill.IsCasting();
				if (num5 > 0f && entity.Skill.IsCasting())
				{
					entity.Skill.TagTrigger();
					entity.Skill.CurrentSkill.MultipleDirectionFactorByServer = num5;
				}
				break;
			}
			}
			//bool flag20 = xstateDefine > XStateDefine.XState_Idle;
			if (xstateDefine > XStateDefine.XState_Idle)
			{
				entity.Net.KillIdle();
			}
			entity.IsPassive = (data.PassiveSpecified && data.Passive);
			entity.Net.CorrectNet(pos, face, num, entity.Skill != null && entity.Skill.Enabled && entity.Machine != null && entity.Machine.Enabled);
		}

		// Token: 0x0600D12F RID: 53551 RVA: 0x003063C8 File Offset: 0x003045C8
		public void OnMoveReceived(XEntity entity, StepMoveData data)
		{
			Vector3 destination = new Vector3((float)(data.PosXZ >> 16) / 100f, 0f, (float)(data.PosXZ & 65535) / 100f);
			XMoveEventArgs @event = XEventPool<XMoveEventArgs>.GetEvent();
			@event.Speed = entity.Attributes.RunSpeed;
			@event.Destination = destination;
			@event.Inertia = data.Stoppage;
			@event.Stoppage = data.Stoppage;
			if (data.Stoppage)
			{
				@event.StopTowards = (float)data.Face / 10f;
			}
			@event.Firer = entity;
			XSingleton<XEventMgr>.singleton.FireEvent(@event);
		}
	}
}
